package cn.zswltech.gruul.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import tk.mybatis.spring.mapper.MapperScannerConfigurer;

import javax.sql.DataSource;
import java.util.Properties;


/**
 * tkmybatis配置
 *
 * @author wangchuanhao
 * @date 2022/6/19 12:24 AM
 */
@Configuration
@MapperScan(basePackages = "cn.zswltech.gruul.dao.dal.dao")
public class TkMybatisConfig {

    //@Bean("gruulTkMapperScannerConfigurer")
    //@ConditionalOnMissingBean(MapperScannerConfigurer.class)
    public MapperScannerConfigurer mapperScannerConfigurer() {
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setBasePackage("cn.zswltech.gruul.dao.dal.dao,cn.zswltech.lib.futurelog.save.mapper");
        //配置通用Mapper，详情请查阅官方文档
        Properties properties = new Properties();
        properties.setProperty("mappers", "cn.zswltech.gruul.dao.dal.tkmybatis.IMapper,cn.zswltech.lib.futurelog.save.util.IMapper");
        properties.setProperty("notEmpty", "false");//insert、update是否判断字符串类型!='' 即 test="str != null"表达式内是否追加 and str != ''
        properties.setProperty("IDENTITY", "MYSQL");
        mapperScannerConfigurer.setProperties(properties);

        return mapperScannerConfigurer;
    }

    @Bean(name = "gruultSqlSessionFactory")
    public SqlSessionFactory moonlightSqlSessionFactory(@Autowired DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
        bean.setConfigLocation(new ClassPathResource("mybatis/mybatis-mysql-config.xml"));
        return bean.getObject();
    }

}

